import $util from "@/utils/index.js"
import $api from "@/api/index.js"
import $store from "@/store/index.js"
import {
	req
} from '@/utils/req.js';
export default {
	state: {
		tradeList: [
			{title: '待服务', number: 0, type: 'wait_count'},
			{title: '已服务', number: 0, type: 'ser_count'},
			{title: '好评数', number: 0, type: 'good_count'},
			{title: '差评数', number: 0, type: 'fail_count'}
		],
		tabList: [{
			title: '全部',
			id: 0,
			//number: 0,
			//type: 'total'
		}, {
			title: '待上门',
			id: 3,
			number: 0,
			type: 'door'
		}, {
			title: '待开始',
			id: 5,
			number: 0,
			type: 'wait'
		}, {
			title: '服务中',
			id: 6,
			number: 0,
			type: 'check'
		}, {
			title: '已完成',
			id: 7,
			//number: 0,
			//type: 'end'
		}, {
			title: '已取消',
			id: '-2'
		}],
		activeIndex: 0,
		list: {
			data: []
		},
		param: {
			page: 1,
			status: 0,
			start_time: '',
			end_time: ''
		},
		pageActive: false
	},
	mutations: {
		//更新内容
		async updateHomeItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		//订单数据（我的数据）
		async getOrderCount({
			commit,
			state
		}, param) {
			let data = await $api.home.orderCount()
			let tradeList = $util.deepCopy(state.tradeList)
			tradeList.forEach(item => {
				item.number = data[item.type]
			})
			commit('updateHomeItem', {
				key: 'tradeList',
				val: tradeList
			})
		},
		//订单列表
		async getHomeOrderList({
			commit,
			state
		}, param) {
			state.pageActive = true
			let {
				orderIndex = 0
			} = param
			let d = '' //await $api.home.getDemandList(param)
			state.list = { data: [] }
			if(orderIndex == 1){
				// param.start_time = $util.DateToUnix($util.formatTime(new Date() , 'YY-M-D') + ' 00:00:00')
				// param.end_time = $util.DateToUnix($util.formatTime(new Date() , 'YY-M-D') + ' 23:59:59')
				delete param.orderIndex
				param.status = param.status == 6 ? 67 : param.status
				d = await $api.home.getDemandList(param)
			}else{
				delete param.orderIndex
				d = await $api.home.orderList(param)
			}
			let tabList = $util.deepCopy(state.tabList)
			tabList.forEach(item => {
				item.number = d.order_count ? d.order_count[item.type] : 0
			})
			let oldList = state.list;
			let newList = d;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList;
			}
			commit('updateHomeItem', {
				key: 'list',
				val: list
			})
			commit('updateHomeItem', {
				key: 'tabList',
				val: tabList
			})
		}
	}
}
